package com.qijian.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itextpdf.tool.xml.html.table.TableData;
import com.qijian.business.domain.ProductionDetail;
import com.qijian.business.domain.ProductionDetailSalary;
import com.qijian.business.domain.dto.ProductionDetailDTO;
import com.qijian.business.domain.query.ProductionDetailQuery;
import com.qijian.business.domain.vo.ProductionDetailVo;
import com.qijian.business.domain.query.ProductionDetailSalaryQuery;
import com.qijian.business.domain.vo.ProductionDetailVo;
import com.qijian.business.service.IProductionDetailService;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 报工Controller
 *
 * @author qijian
 * @date 2025-03-25
 */
@Api(tags = "报工")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/productionDetail")
public class ProductionDetailController extends BaseController {

    private final IProductionDetailService productionDetailService;

    /**
     * 查询报工列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductionDetailQuery query, Page<ProductionDetail> page) {
        Page<ProductionDetailVo> pageVo = productionDetailService.pageVo(page, query);
        return getDataTable(pageVo);
    }

    /**
     * 导出报工列表
     */
    @Log(title = "报工" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(ProductionDetailQuery query, HttpServletResponse response) {
        new ExcelUtil<>(ProductionDetail. class).exportExcel(response,productionDetailService.list( WhereEntityTool.invoke(query)), "报工数据");
    }

    /**
     * 获取报工详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(productionDetailService.getById(id));
    }

    /**
     * 海达报工功能
     * @param productionDetailDTO
     * @return
     */
    @PostMapping("saveProductionDetail")
    public AjaxResult saveProductionDetail(@RequestBody ProductionDetailDTO productionDetailDTO) {
        Boolean result = productionDetailService.saveProductionDetail(productionDetailDTO);
        return AjaxResult.success(result);
    }

    /**
     * 查询报工工资列表(日工资)
     */
    @GetMapping("/listDay")
    public TableDataInfo listDay(ProductionDetailQuery query, Page<ProductionDetail> page) {

        return getDataTable(productionDetailService.pageListDay(page, query));
    }

    /**
     * 查询报工工资列表(月工资)
     */
    @GetMapping("/listMoon")
    public TableDataInfo listMoon(ProductionDetailQuery query, Page<ProductionDetail> page) {

        return getDataTable(productionDetailService.pageListMoon(page, query));
    }

    /**
     * 查询报工工资列表(日工资)
     */
    @GetMapping("/listDaySum")
    public TableDataInfo listDaySum(ProductionDetailSalaryQuery query) {

        return getDataTable(productionDetailService.listDaySum(query));
    }

    /**
     * 查询报工工资列表(月工资)
     */
    @GetMapping("/listMoonSalary")
    public AjaxResult listMoonSalary(ProductionDetailSalaryQuery query) {

        return AjaxResult.success(productionDetailService.listMoonSalary(query));
    }

    /**
     * 查询报工工资列表(月工资折线图)
     */
    @GetMapping("/listMoonSum")
    public AjaxResult listMoonSum(ProductionDetailSalaryQuery query) {

        return AjaxResult.success(productionDetailService.listMoonSum(query));
    }


    /**
     * 新增报工
     */
    @Log(title = "报工" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionDetail productionDetail) {
        return toAjax(productionDetailService.save(productionDetail));
    }

    /**
     * 修改报工
     */
    @Log(title = "报工" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionDetail productionDetail) {
        approval(productionDetailService.getById(productionDetail.getId()));
        return toAjax(productionDetailService.updateById(productionDetail));
    }

    /**
     * 删除报工
     */
    @Log(title = "报工" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        ids.forEach(id -> approval(productionDetailService.getById(id)));
        return toAjax(productionDetailService.removeByIds(ids));
    }
}
